package cn.edu.neusoft.zengweidong707.neufood.activity;


import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import cn.edu.neusoft.zengweidong707.neufood.R;
import cn.edu.neusoft.zengweidong707.neufood.bean.LoginBean;
import cn.edu.neusoft.zengweidong707.neufood.listener.ZwdListener;
import cn.edu.neusoft.zengweidong707.neufood.model.UserModel;

public class LoginActivity extends BaseActivity {
    private Intent intent;
    private EditText et_username;
    private EditText et_userpass;
    private CheckBox cb_remenberuser;
    private Button btn_login;
    private TextView tv_forgetpassword;
    private TextView tv_phonereg;
    private TextView tv_reg;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    private String username;
    private String password;
    private UserModel model;
    private ZwdListener<LoginBean> listener;
    private boolean remember;


    @Override
    void initView() {
        setLayout_file(R.layout.activity_login);
        et_username = (EditText) findViewById(R.id.et_username);
        et_userpass = (EditText) findViewById(R.id.et_userpass);
        cb_remenberuser = (CheckBox) findViewById(R.id.cb_remenberuser);
        btn_login = (Button) findViewById(R.id.btn_login);
        tv_forgetpassword = (TextView) findViewById(R.id.tv_forgetpassword);
        tv_phonereg = (TextView) findViewById(R.id.tv_phonereg);
        tv_reg = (TextView) findViewById(R.id.tv_reg);

        et_username.setText(getIntent().getStringExtra("name"));
        intent = new Intent(LoginActivity.this, MainActivity.class);
        sp = getSharedPreferences("userInfo", MODE_PRIVATE);
        editor = sp.edit();
        remember = getRemember();
        username = getUsername();
        password = getUserpass();
        if (remember == true) {
            et_username.setText(username);
            et_userpass.setText(password);
        }
    }

    @Override
    void initEvent() {
        listener = new ZwdListener<LoginBean>() {
            @Override
            public void onResponse(LoginBean l) {
                if (l.getUserid().equals("0")) {
                    showToast("用户名或密码错误");
                } else {
                    editor.putString("username", et_username.getText().toString());
                    editor.putString("userpass", et_userpass.getText().toString());
                    editor.putString("userid", l.getUserid());
                    editor.putBoolean("remember", cb_remenberuser.isChecked());
                    editor.commit();
                    startActivity(intent);
                    finish();
                }
            }

            @Override
            public void onFail(String msg) {
                showToast(msg);
            }
        };

        tv_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                intent = new Intent(LoginActivity.this,RegActivity.class);
                startActivity(intent);
                finish();
            }
        });

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                model = new UserModel();
                model.login(et_username.getText().toString(),
                        et_userpass.getText().toString(),
                        listener);
            }
        });
    }

    @Override
    void initData() {

    }

}

